//
//  UIColor+BLColor.m
//  BLExtension
//
//  Created by huangbolun on 2018/1/11.
//  Copyright © 2018年 huangbolun. All rights reserved.
//

#import "UIColor+BLColor.h"

@implementation UIColor (BLColor)

+ (UIColor *)bl_colorWithR:(NSInteger)red g:(NSInteger)green b:(NSInteger)blue a:(CGFloat)alpha{
    return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:alpha];
}

+ (UIColor *)bl_colorRandom {
    return [UIColor colorWithRed:(arc4random()%256)/255.0 green:(arc4random()%256)/255.0 blue:(arc4random()%256)/255.0 alpha:1.0];
}

+ (UIColor *)colorWithHexString:(NSString *)stringToConvert{
    if([stringToConvert hasPrefix:@"#"])
    {
        stringToConvert = [stringToConvert substringFromIndex:1];
    }else if([stringToConvert hasPrefix:@"0x"]){
        [stringToConvert stringByReplacingOccurrencesOfString:@"0x" withString:@""];
    }
    
    NSScanner *scanner = [NSScanner scannerWithString:stringToConvert];
    
    unsigned hexNum;
    if(![scanner scanHexInt:&hexNum])
    {
        return nil;
    }
    return[self colorWithRGBHex:hexNum];
}

+ (UIColor *)colorWithRGBHex:(UInt32)hex{
    int r = (hex >>16) &0xFF;
    int g = (hex >>8) &0xFF;
    int b = (hex) &0xFF;
    return[UIColor colorWithRed:r /255.0f
                          green:g /255.0f
                           blue:b /255.0f
                          alpha:1.0f];
}

@end
